package tech.senri.iot.socket.service

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tech.senri.iot.socket.model.*

@Component
class RequestMessageEncoder
{
    private val updatePriceInventoryRequest = Pair(0x70, 0)
    private val updatePriceInventoryConfirmRequest = Pair(0x71, 0x01)
    private val updateAislesCapacityRequest = Pair(0x10, 0x0F)
    private val updateDiscountRequest = Pair(0x10, 0x10)
    private val updateMergeAisleRequest = Pair(0x10, 0x11)
    private val deliverCommodityRequest = Pair(0x71, 0x04)
    private val remoteTestRequest = Pair(0x71, 0x05)

    @Autowired
    private lateinit var objectMapper: ObjectMapper

    fun encode(code: Pair<Int, Int>, msg: String): RequestMessage?
    {
        var result: RequestMessage? = null
        when (code)
        {
            updatePriceInventoryRequest        -> result = objectMapper.readValue<UpdatePriceInventoryRequest>(msg)
            updatePriceInventoryConfirmRequest -> result = objectMapper.readValue<UpdatePriceInventoryConfirmRequest>(msg)
            updateAislesCapacityRequest        -> result = objectMapper.readValue<UpdateAislesCapacityRequest>(msg)
            updateDiscountRequest              -> result = objectMapper.readValue<UpdateDiscountRequest>(msg)
            updateMergeAisleRequest            -> result = objectMapper.readValue<UpdateMergeAisleRequest>(msg)
            deliverCommodityRequest            -> result = objectMapper.readValue<DeliverCommodityRequest>(msg)
            remoteTestRequest                  -> result = objectMapper.readValue<RemoteTestRequest>(msg)
        }
        return result
    }
}
